import { asyncRoutes } from '@/router'
import Layout from '@/layout'
// 静态路由数组
const constRoutes = [
    {
        path: '/',
        component: Layout,
        name: 'dashboard',
        meta: { title: '面板' },
        redirect: '/dashboard',
        children: [
            {
                path: '/dashboard',
                name: 'dashboard',
                component: () => import('@/views/dashboard'),
                meta: { title: '面板' }
            }

        ]
    },
    {
        path: '/login',
        component: () => import("@/views/login"),
        meta: { title: '登录' },
        hidden: true

    },

]

export default {
    namespaced: true,
    state() {
        return {
            routes: constRoutes
        }
    },
    getters: {

    },
    mutations: {
        // 5、设置给完成的路由映射变量
        setRoutes(state, newRoutes) {
            // 静态路由+动态路由
            state.routes = [...constRoutes, ...newRoutes]
        }

    },
    actions: {
        // 4、通过权限菜单标识，筛选出该用户的动态路由
        filterRoutes(context, data) {
            // data就是服务器返回的该用户的信息对象
            // 遍历所有动态路由映射，判断他的name是不是在权限标识数组中
            // 把用户name放入userRoutes中，然后调用setRoutes方法
            // 把动态路由存储到vuex中
            const userRoutes = []
            asyncRoutes.forEach(item => {
                if (data.includes(item.name)) userRoutes.push(item)
            })
            context.commit('setRoutes', userRoutes)
            return userRoutes
        }

    }


}